<template>
    <router-link to="/" class="header-logo">
        <img class="logo" src="../../assets/svg/logo.svg" />
        <h1 class="name">学了么</h1>
    </router-link>
    <div class="admin-login">
        <div class="admin-login-wrap">
            <div class="admin-login-left">
                <div class="slogan">
                    <h2>管理员控制台</h2>
                    <p>🎯 系统管理 | 数据监控 | 权限控制</p>
                    <p style="margin-top: 12px; font-size: 1.1rem; opacity: 0.85;">安全访问，专业管理</p>
                </div>
            </div>
            <div class="admin-login-right">
                <div class="admin-login-right-wrap">
                    <h3 class="title">管理员登录</h3>
                    <form @submit.prevent="handleLogin" class="login-form">
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input 
                                type="text" 
                                id="username" 
                                v-model="loginForm.username" 
                                placeholder="请输入管理员用户名"
                                required
                            />
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input 
                                type="password" 
                                id="password" 
                                v-model="loginForm.password" 
                                placeholder="请输入密码"
                                required
                            />
                        </div>
                        <button type="submit" class="login-btn" :disabled="loading">
                            {{ loading ? '登录中...' : '登录' }}
                        </button>
                    </form>
                    <!-- 用户登录入口 -->
                    <div class="user-entrance">
                        <div class="user-divider">
                            <span>或</span>
                        </div>
                        <div class="user-login-btn" @click="goToUserLogin">
                            <icon-user />
                            用户登录
                        </div>
                    </div>
                    <div class="admin-tips">
                        <p>管理员专用登录入口</p>
                        <p>如有问题请联系系统管理员</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { adminLoginRequest } from '../../apis/auth-api'
import { Notification } from '@arco-design/web-vue'
import { IconUser } from '@arco-design/web-vue/es/icon'

const router = useRouter()
const loading = ref(false)

const loginForm = reactive({
    username: '',
    password: ''
})

const handleLogin = async () => {
    if (!loginForm.username || !loginForm.password) {
        Notification.warning({
            title: '提示',
            content: '请输入用户名和密码'
        })
        return
    }

    loading.value = true
    try {
        const response = await adminLoginRequest(loginForm.username, loginForm.password)
        
        // 根据项目的响应格式判断是否成功
        if (response.data && response.data.code && response.data.code.startsWith('0')) {
            // 存储管理员信息到 localStorage
            localStorage.setItem('adminToken', response.data.data?.token || 'admin-token')
            localStorage.setItem('adminInfo', JSON.stringify(response.data.data || {username: loginForm.username}))
            
            Notification.success({
                title: '登录成功',
                content: response.data.msg || '欢迎使用管理员控制台'
            })
            
            // 跳转到管理员功能页面
            router.push('/admin/dashboard')
        } else {
            Notification.error({
                title: '登录失败',
                content: response.data?.msg || '用户名或密码错误'
            })
        }
    } catch (error) {
        console.error('管理员登录错误:', error)
        Notification.error({
            title: '登录失败',
            content: error.response?.data?.msg || '网络错误，请稍后重试'
        })
    } finally {
        loading.value = false
    }
}

// 跳转到用户登录
const goToUserLogin = () => {
    router.push('/login')
}
</script>

<style lang="less" scoped>
.header-logo {
    position: fixed;
    top: 40px;
    left: 40px;
    display: flex;
    align-items: center;
    margin-right: 20px;
    z-index: 1000;

    .logo {
        height: 30px;
        width: 30px;
        padding: 5px;
        background-color: var(--color-bg-1);
        border-radius: 5px;
    }

    .name {
        text-align: center;
        font-weight: bold;
        font-size: 24px;
        margin-left: 10px;
        color: #fff;
    }
}

.admin-login {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    position: relative;
    overflow: hidden;
    
    &::before {
        content: '';
        position: absolute;
        top: -50%;
        right: -50%;
        width: 100%;
        height: 100%;
        background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
        animation: rotate 20s linear infinite;
    }

    .admin-login-wrap {
        min-height: 100vh;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        position: relative;
        z-index: 1;

        .admin-login-left,
        .admin-login-right {
            flex: 1;
        }

        .admin-login-left {
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;

            .slogan {
                text-align: center;
                position: relative;
                
                &::before {
                    content: '🔐';
                    font-size: 80px;
                    display: block;
                    margin-bottom: 30px;
                    animation: float 3s ease-in-out infinite;
                }
                
                h2 {
                    font-size: 3rem;
                    font-weight: 700;
                    margin-bottom: 24px;
                    text-shadow: 0 4px 8px rgba(0,0,0,0.3);
                    letter-spacing: 2px;
                }
                
                p {
                    font-size: 1.3rem;
                    opacity: 0.95;
                    line-height: 1.6;
                    text-shadow: 0 2px 4px rgba(0,0,0,0.2);
                }
            }
        }

        .admin-login-right {
            display: flex;
            justify-content: center;
            box-sizing: border-box;
            width: 100%;

            .admin-login-right-wrap {
                border-radius: 20px;
                padding: 60px;
                background: var(--color-bg-1);
                box-shadow: 0 20px 40px rgba(0,0,0,0.1);
                width: 400px;
                backdrop-filter: blur(10px);

                .title {
                    font-size: 24px;
                    font-weight: 600;
                    line-height: 30px;
                    text-align: center;
                    color: var(--color-text-2);
                    margin-bottom: 40px;
                }

                .login-form {
                    .form-group {
                        margin-bottom: 24px;

                        label {
                            display: block;
                            margin-bottom: 8px;
                            font-weight: 500;
                            color: var(--color-text-2);
                        }

                        input {
                            width: 100%;
                            padding: 12px 16px;
                            border: 1px solid var(--color-border-2);
                            border-radius: 8px;
                            font-size: 14px;
                            transition: all 0.3s;
                            box-sizing: border-box;

                            &:focus {
                                outline: none;
                                border-color: rgb(var(--primary-6));
                                box-shadow: 0 0 0 3px rgba(var(--primary-6), 0.1);
                            }

                            &::placeholder {
                                color: var(--color-text-3);
                            }
                        }
                    }

                    .login-btn {
                        width: 100%;
                        padding: 14px;
                        background: linear-gradient(135deg, rgb(var(--primary-6)) 0%, rgb(var(--primary-7)) 100%);
                        color: white;
                        border: none;
                        border-radius: 8px;
                        font-size: 16px;
                        font-weight: 500;
                        cursor: pointer;
                        transition: all 0.3s;

                        &:hover:not(:disabled) {
                            transform: translateY(-2px);
                            box-shadow: 0 8px 20px rgba(var(--primary-6), 0.3);
                        }

                        &:disabled {
                            opacity: 0.6;
                            cursor: not-allowed;
                        }
                    }
                }

                .user-entrance {
                    margin-top: 30px;
                    
                    .user-divider {
                        display: flex;
                        align-items: center;
                        text-align: center;
                        margin: 20px 0;
                        
                        &::before,
                        &::after {
                            content: '';
                            flex: 1;
                            border-bottom: 1px solid var(--color-border-2);
                        }
                        
                        span {
                            padding: 0 15px;
                            color: var(--color-text-3);
                            font-size: 14px;
                        }
                    }
                    
                    .user-login-btn {
                        width: 100%;
                        padding: 14px;
                        background: transparent;
                        color: rgb(var(--primary-6));
                        border: 2px solid rgb(var(--primary-6));
                        border-radius: 8px;
                        font-size: 16px;
                        font-weight: 600;
                        cursor: pointer;
                        transition: all 0.3s;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        gap: 8px;
                        
                        &:hover {
                            background: rgb(var(--primary-6));
                            color: white;
                            transform: translateY(-2px);
                            box-shadow: 0 8px 20px rgba(var(--primary-6), 0.3);
                        }
                    }
                }
                
                .admin-tips {
                    margin-top: 30px;
                    text-align: center;
                    
                    p {
                        color: var(--color-text-3);
                        font-size: 12px;
                        margin: 4px 0;
                    }
                }
            }
        }
    }
}

@keyframes float {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-15px);
    }
}

@keyframes rotate {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

@media screen and (max-width: 768px) {
    .admin-login {
        .admin-login-wrap {
            flex-direction: column;
            padding: 20px;

            .admin-login-left {
                margin-bottom: 40px;
                
                .slogan {
                    &::before {
                        font-size: 60px;
                    }
                    
                    h2 {
                        font-size: 2rem;
                    }
                    
                    p {
                        font-size: 1rem;
                    }
                }
            }

            .admin-login-right-wrap {
                width: 100%;
                max-width: 400px;
                padding: 40px 30px;
            }
        }
    }
}
</style>